{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 前期准备\n",
    "+ 在桌面新建文件夹用于保存验证码图片(C:\\Users\\dell\\Desktop\\img\")\n",
    "+ 调配电脑显示为100%（设置中调配）\n",
    "+ 在D:\\知网_pdf，新建文件夹（保存pdf文档）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-1-32475f07e653>:11: DeprecationWarning: use options instead of chrome_options\n",
      "  driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.desired_capabilities import DesiredCapabilities\n",
    "opts = webdriver.ChromeOptions()\n",
    "opts.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错\n",
    "opts.add_argument('window-size=1920x3000') #指定浏览器分辨率\n",
    "opts.add_argument('--disable-gpu') #谷歌文档提到需要加上一这个属性来规避bug\n",
    "opts.add_argument('--hide-scrollbars') #隐藏滚动条, 应对些特殊页面\n",
    "out_path = r'D:\\知网_pdf'  # 是你想指定的路径\n",
    "prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': out_path}\n",
    "opts.add_experimental_option('prefs', prefs)\n",
    "driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver.support.wait import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.webdriver.common.by import By\n",
    "from time import sleep\n",
    "import pandas as pd\n",
    "import requests\n",
    "import time\n",
    "import hashlib\n",
    "import base64\n",
    "import json\n",
    "import os\n",
    "from PIL import Image "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get('https://cnki.net')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "element=driver.find_element_by_xpath('//*[@id=\"Ecp_loginShowName1\"]')\n",
    "element.get_attribute('innerHTML')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 高级检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "search = driver.find_element_by_xpath('//*[@id=\"highSearch\"]').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 检查窗口信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'CDwindow-E7664240837C2E3151704B9CFB5ABF1F'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.current_window_handle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['CDwindow-E7664240837C2E3151704B9CFB5ABF1F',\n",
       " 'CDwindow-9BD79939D690E33296639ECFDE93C1C1']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.window_handles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-8-6c6d5ce6602d>:1: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[1])\n"
     ]
    }
   ],
   "source": [
    "driver.switch_to_window(driver.window_handles[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 选择专业检索\n",
    "*放慢点击速度，以防selenium加载报错*    \n",
    "*4、5出错则从此重新运行*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "点击学术期刊 = driver.find_element_by_xpath('/html/body/div[3]/div[1]/div/ul[1]/li[1]/a')\n",
    "点击学术期刊.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "专业检索 = driver.find_element_by_xpath('//li[@name=\"majorSearch\"]')\n",
    "专业检索.get_attribute(\"textContent\")\n",
    "专业检索.click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输入关键词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "query='SU=\"新媒体传播\"'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "检索 = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div[1]/div[1]/div[2]/textarea')\n",
    "检索.clear()\n",
    "检索.send_keys(query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 选择c刊和核心期刊"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_xpath('//input[@key=\"HX\"]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_xpath('//input[@key=\"CSI\"]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_xpath('//input[@value=\"检索\"]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>篇名</th>\n",
       "      <th>作者</th>\n",
       "      <th>刊名</th>\n",
       "      <th>发表时间</th>\n",
       "      <th>被引</th>\n",
       "      <th>下载</th>\n",
       "      <th>操作</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>新媒体视野下常德丝弦的创新与传播研究</td>\n",
       "      <td>田正铁</td>\n",
       "      <td>四川戏剧</td>\n",
       "      <td>2021-06-25 14:42</td>\n",
       "      <td>NaN</td>\n",
       "      <td>118</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>互联网新媒体传播中农村职业培训脱贫致富基本内涵的逻辑生成</td>\n",
       "      <td>杨宗晓; 杨克</td>\n",
       "      <td>农业经济</td>\n",
       "      <td>2021-06-04</td>\n",
       "      <td>NaN</td>\n",
       "      <td>137</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>互联网新媒体传播中农村职业培训脱贫致富基本内涵的逻辑生成</td>\n",
       "      <td>杨宗晓; 杨克</td>\n",
       "      <td>农业经济</td>\n",
       "      <td>2021-06-04</td>\n",
       "      <td>NaN</td>\n",
       "      <td>137</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>新媒体传播范式下广告传播的新形态及问题探讨——以KOL营销传播为例</td>\n",
       "      <td>王蕊</td>\n",
       "      <td>新闻爱好者</td>\n",
       "      <td>2021-05-20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>311</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>新媒体视阈下戏曲艺术传播方式研究——评《20世纪戏曲传播方式研究》</td>\n",
       "      <td>江南</td>\n",
       "      <td>新闻爱好者</td>\n",
       "      <td>2021-05-20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>103</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>新媒体传播范式下广告传播的新形态及问题探讨——以KOL营销传播为例</td>\n",
       "      <td>王蕊</td>\n",
       "      <td>新闻爱好者</td>\n",
       "      <td>2021-05-20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>311</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>新媒体视阈下戏曲艺术传播方式研究——评《20世纪戏曲传播方式研究》</td>\n",
       "      <td>江南</td>\n",
       "      <td>新闻爱好者</td>\n",
       "      <td>2021-05-20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>103</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>新媒体传播在饲料企业广告中的应用</td>\n",
       "      <td>何炜</td>\n",
       "      <td>中国饲料</td>\n",
       "      <td>2021-05-20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>后疫情时代改善与重塑国家形象的新媒体传播策略</td>\n",
       "      <td>匡文波; 马茜茜</td>\n",
       "      <td>新闻与写作</td>\n",
       "      <td>2021-05-05</td>\n",
       "      <td>NaN</td>\n",
       "      <td>863</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>后疫情时代改善与重塑国家形象的新媒体传播策略</td>\n",
       "      <td>匡文波; 马茜茜</td>\n",
       "      <td>新闻与写作</td>\n",
       "      <td>2021-05-05</td>\n",
       "      <td>NaN</td>\n",
       "      <td>863</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>新时代中国博物馆行业传播工作的发展</td>\n",
       "      <td>张雪嫣</td>\n",
       "      <td>出版广角</td>\n",
       "      <td>2021-04-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>56</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>后疫情时代城市传播受到的挑战与发展趋向</td>\n",
       "      <td>党东耀; 王司媛</td>\n",
       "      <td>青年记者</td>\n",
       "      <td>2021-04-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13</td>\n",
       "      <td>后疫情时代城市传播受到的挑战与发展趋向</td>\n",
       "      <td>党东耀; 王司媛</td>\n",
       "      <td>青年记者</td>\n",
       "      <td>2021-04-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>242</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14</td>\n",
       "      <td>新时代中国博物馆行业传播工作的发展</td>\n",
       "      <td>张雪嫣</td>\n",
       "      <td>出版广角</td>\n",
       "      <td>2021-04-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>56</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15</td>\n",
       "      <td>2021省级卫视春晚融合传播监测与分析</td>\n",
       "      <td>宫瑜璟; 李多</td>\n",
       "      <td>传媒</td>\n",
       "      <td>2021-04-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>63</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>数字媒体时代传播学的生态系统探究——评《新媒体:传播新生态构建》</td>\n",
       "      <td>夏晶晶</td>\n",
       "      <td>中国科技论文</td>\n",
       "      <td>2021-04-15</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17</td>\n",
       "      <td>数字媒体时代传播学的生态系统探究——评《新媒体:传播新生态构建》</td>\n",
       "      <td>夏晶晶</td>\n",
       "      <td>中国科技论文</td>\n",
       "      <td>2021-04-15</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18</td>\n",
       "      <td>新媒体环境下影视广告的现状与改善对策——评《影视广告学》</td>\n",
       "      <td>李树威</td>\n",
       "      <td>热带作物学报</td>\n",
       "      <td>2021-03-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>98</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19</td>\n",
       "      <td>新媒体传播与大学生思想政治教育创新途径——评《新时代高校思想政治教育理念与实践》</td>\n",
       "      <td>刘旭光</td>\n",
       "      <td>热带作物学报</td>\n",
       "      <td>2021-03-25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>48</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>现状与特征:社会网络分析在我国传播学研究中的应用</td>\n",
       "      <td>瞿旭晟; 赵鹏程</td>\n",
       "      <td>新闻爱好者</td>\n",
       "      <td>2021-03-20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>439</td>\n",
       "      <td>下载</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Unnamed: 0                                        篇名        作者      刊名  \\\n",
       "0            1                        新媒体视野下常德丝弦的创新与传播研究       田正铁    四川戏剧   \n",
       "1            2              互联网新媒体传播中农村职业培训脱贫致富基本内涵的逻辑生成   杨宗晓; 杨克    农业经济   \n",
       "2            3              互联网新媒体传播中农村职业培训脱贫致富基本内涵的逻辑生成   杨宗晓; 杨克    农业经济   \n",
       "3            4         新媒体传播范式下广告传播的新形态及问题探讨——以KOL营销传播为例        王蕊   新闻爱好者   \n",
       "4            5         新媒体视阈下戏曲艺术传播方式研究——评《20世纪戏曲传播方式研究》        江南   新闻爱好者   \n",
       "5            6         新媒体传播范式下广告传播的新形态及问题探讨——以KOL营销传播为例        王蕊   新闻爱好者   \n",
       "6            7         新媒体视阈下戏曲艺术传播方式研究——评《20世纪戏曲传播方式研究》        江南   新闻爱好者   \n",
       "7            8                          新媒体传播在饲料企业广告中的应用        何炜    中国饲料   \n",
       "8            9                    后疫情时代改善与重塑国家形象的新媒体传播策略  匡文波; 马茜茜   新闻与写作   \n",
       "9           10                    后疫情时代改善与重塑国家形象的新媒体传播策略  匡文波; 马茜茜   新闻与写作   \n",
       "10          11                         新时代中国博物馆行业传播工作的发展       张雪嫣    出版广角   \n",
       "11          12                       后疫情时代城市传播受到的挑战与发展趋向  党东耀; 王司媛    青年记者   \n",
       "12          13                       后疫情时代城市传播受到的挑战与发展趋向  党东耀; 王司媛    青年记者   \n",
       "13          14                         新时代中国博物馆行业传播工作的发展       张雪嫣    出版广角   \n",
       "14          15                       2021省级卫视春晚融合传播监测与分析   宫瑜璟; 李多      传媒   \n",
       "15          16          数字媒体时代传播学的生态系统探究——评《新媒体:传播新生态构建》       夏晶晶  中国科技论文   \n",
       "16          17          数字媒体时代传播学的生态系统探究——评《新媒体:传播新生态构建》       夏晶晶  中国科技论文   \n",
       "17          18              新媒体环境下影视广告的现状与改善对策——评《影视广告学》       李树威  热带作物学报   \n",
       "18          19  新媒体传播与大学生思想政治教育创新途径——评《新时代高校思想政治教育理念与实践》       刘旭光  热带作物学报   \n",
       "19          20                  现状与特征:社会网络分析在我国传播学研究中的应用  瞿旭晟; 赵鹏程   新闻爱好者   \n",
       "\n",
       "                发表时间  被引   下载  操作  \n",
       "0   2021-06-25 14:42 NaN  118  下载  \n",
       "1         2021-06-04 NaN  137  下载  \n",
       "2         2021-06-04 NaN  137  下载  \n",
       "3         2021-05-20 NaN  311  下载  \n",
       "4         2021-05-20 NaN  103  下载  \n",
       "5         2021-05-20 NaN  311  下载  \n",
       "6         2021-05-20 NaN  103  下载  \n",
       "7         2021-05-20 NaN   15  下载  \n",
       "8         2021-05-05 NaN  863  下载  \n",
       "9         2021-05-05 NaN  863  下载  \n",
       "10        2021-04-30 NaN   56  下载  \n",
       "11        2021-04-30 NaN  242  下载  \n",
       "12        2021-04-30 NaN  242  下载  \n",
       "13        2021-04-30 NaN   56  下载  \n",
       "14        2021-04-25 NaN   63  下载  \n",
       "15        2021-04-15 NaN   23  下载  \n",
       "16        2021-04-15 NaN   23  下载  \n",
       "17        2021-03-25 NaN   98  下载  \n",
       "18        2021-03-25 NaN   48  下载  \n",
       "19        2021-03-20 NaN  439  下载  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "element = driver.find_element_by_xpath('//div[@id=\"gridTable\"]')\n",
    "网页内容 = element.get_attribute('innerHTML')\n",
    "df=pd.read_html(网页内容)[0]\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 保存下载文章的验证码代码（不用运行）\n",
    "*运行前需更改应用文本在电脑显示为100%*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding:utf-8\n",
    "from selenium import webdriver\n",
    "from PIL import Image\n",
    "##截图\n",
    "driver.save_screenshot (r\"C:\\Users\\dell\\Desktop\\img\\下载.png\")\n",
    "#2.定位到验证码图片元素\n",
    "code_ele =driver. find_element_by_xpath ('//*[@id=\"vImg\"]')\n",
    "#3.元素的位置，结果：P'y';,x:},为图片左上角的位置\n",
    "print(code_ele.location)\n",
    "#4.元素的大小，结果：｛＇height';,'width':}\n",
    "print(code_ele.size)\n",
    "#5.得到将元素的具体位置\n",
    "x0 = code_ele.location[\"x\"] +120\n",
    "y0=code_ele.location[\"y\"] +70\n",
    "x1 = code_ele.size[\"width\"] + x0 + 120\n",
    "y1=code_ele.size[\"height\"] +y0\n",
    "img = Image.open(open (r\"C:\\Users\\dell\\Desktop\\img\\下载.png\",'rb'))\n",
    "image = img.crop((x0,y0,x1,y1))#左、上、右、下\n",
    "image.save(r\"C:\\Users\\dell\\Desktop\\img\\验证码＿下载.png\")#将验证码图片保存为code_img.png"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### api识别代码（不用运行）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import time\n",
    "import hashlib\n",
    "import base64\n",
    "import json\n",
    "def parse_img(APPID,API_KEY):\n",
    "    curTime = str(int(time.time()))\n",
    "#  支持语言类型和是否开启位置定位(默认否)\n",
    "    param = {\"language\": \"cn|en\", \"location\": \"false\"}\n",
    "    param = json.dumps(param)\n",
    "    paramBase64 = base64.b64encode(param.encode('utf-8'))\n",
    "\n",
    "    m2 = hashlib.md5()\n",
    "    str1 = API_KEY + curTime + str(paramBase64,'utf-8')\n",
    "    m2.update(str1.encode('utf-8'))\n",
    "    checkSum = m2.hexdigest()\n",
    "\n",
    "    # 组装http请求头\n",
    "    header = {\n",
    "        'X-CurTime': curTime,\n",
    "        'X-Param': paramBase64,\n",
    "        'X-Appid': APPID,\n",
    "        'X-CheckSum': checkSum,\n",
    "        'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',}\n",
    "    URL = \"http://webapi.xfyun.cn/v1/service/v1/ocr/general\"\n",
    "    with open(r'C:\\Users\\dell\\Desktop\\img\\验证码＿下载.png','rb') as f:\n",
    "            img = f.read()\n",
    "            img = str(base64.b64encode(img), 'utf-8')\n",
    "    data = {'image': img}\n",
    "    r = requests.post(URL, data=data, headers=header)\n",
    "    ret = str(r.content, 'utf-8')\n",
    "    result = json.loads(ret)['data']['block'][0]['line'][0]['word'][0]['content']\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'M4C'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse_img(\"427b1be6\",\"ba65f1334305e5cb63a4da6d3f2071f8\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 将验证码识别过程封装为函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def verification_code():\n",
    "    ###循环10次一般可以解决验证码，不够可再加\n",
    "    for i in range(0,10):\n",
    "        ##截图（创建文件夹用于保存图片）\n",
    "        driver.save_screenshot (r\"C:\\Users\\dell\\Desktop\\img\\下载.png\")\n",
    "        #2.定位到验证码图片元素(调整位置)\n",
    "        code_ele =driver.find_element_by_xpath ('//*[@id=\"vImg\"]')\n",
    "        x0 = code_ele.location[\"x\"] \n",
    "        y0=code_ele.location[\"y\"] \n",
    "        x1 = code_ele.size[\"width\"] + x0 \n",
    "        y1=code_ele.size[\"height\"] +y0\n",
    "        img = Image.open(open (r\"C:\\Users\\dell\\Desktop\\img\\下载.png\",'rb'))\n",
    "        image = img.crop((x0,y0,x1,y1))#左、上、右、下\n",
    "        image.save(r\"C:\\Users\\dell\\Desktop\\img\\验证码＿下载.png\")#将验证码图片保存为code_img.png为code_img.png\n",
    "    #调用api\n",
    "        sleep(1)\n",
    "        APPID=\"427b1be6\"\n",
    "        API_KEY=\"ba65f1334305e5cb63a4da6d3f2071f8\"\n",
    "        curTime = str(int(time.time()))\n",
    "    #  支持语言类型和是否开启位置定位(默认否)\n",
    "        param = {\"language\": \"cn|en\", \"location\": \"false\"}\n",
    "        param = json.dumps(param)\n",
    "        paramBase64 = base64.b64encode(param.encode('utf-8'))\n",
    "        m2 = hashlib.md5()\n",
    "        str1 = API_KEY + curTime + str(paramBase64,'utf-8')\n",
    "        m2.update(str1.encode('utf-8'))\n",
    "        checkSum = m2.hexdigest()\n",
    "\n",
    "    # 组装http请求头\n",
    "        header = {\n",
    "            'X-CurTime': curTime,\n",
    "            'X-Param': paramBase64,\n",
    "            'X-Appid': APPID,\n",
    "            'X-CheckSum': checkSum,\n",
    "            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',}\n",
    "        URL = \"http://webapi.xfyun.cn/v1/service/v1/ocr/general\"\n",
    "        with open(r'C:\\Users\\dell\\Desktop\\img\\验证码＿下载.png','rb') as f:\n",
    "            img = f.read()\n",
    "            img = str(base64.b64encode(img), 'utf-8')\n",
    "            data = {'image': img}\n",
    "        r = requests.post(URL, data=data, headers=header)\n",
    "        ret = str(r.content, 'utf-8')\n",
    "        ### 判断api输入的值是否符合条件（可能只有3位数，导致错误）\n",
    "        try:\n",
    "            check_download_01 = len(files)\n",
    "            result=json.loads(ret)['data']['block'][0]['line'][0]['word'][0]['content']\n",
    "            线框验证码=driver.find_element_by_xpath('//*[@id=\"vcode\"]')\n",
    "            线框验证码.clear()\n",
    "            线框验证码.send_keys(result)\n",
    "            driver.find_element_by_xpath('/html/body/div/form/dl/dd/button').click()\n",
    "            sleep(3)\n",
    "            check_download_02 = len(files)\n",
    "            if check_download_02>check_download_01:\n",
    "                driver.close()\n",
    "                sleep(1)\n",
    "                driver.switch_to_window(driver.window_handles[2])\n",
    "                sleep(1)\n",
    "                driver.switch_to_window(driver.window_handles[1])\n",
    "                break\n",
    "            else:\n",
    "                try:\n",
    "                    driver.find_element_by_xpath('//*[@id=\"vImg\"]').click()\n",
    "                except:##下载失败跳出循环\n",
    "                    driver.close()\n",
    "                    sleep(1)\n",
    "                    driver.switch_to_window(driver.window_handles[2])\n",
    "                    sleep(1)\n",
    "                    driver.close()\n",
    "                    driver.switch_to_window(driver.window_handles[1])\n",
    "                    break\n",
    "         ### 不符合则点击换验证码      \n",
    "        except:\n",
    "            driver.find_element_by_xpath('//*[@id=\"vImg\"]').click()\n",
    "    return"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最终代码\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "driver.switch_to_window(driver.window_handles[1])\n",
    "driver.find_element_by_xpath('//*[@id=\"perPageDiv\"]/div').click()\n",
    "driver.find_element_by_xpath('//*[@id=\"perPageDiv\"]/ul/li[3]').click()\n",
    "element = driver.find_element_by_xpath('//span[@class=\"total\"]')\n",
    "max_page=int(element.get_attribute(\"textContent\").replace(\"共\",\"\").replace(\"页\",\"\"))\n",
    "for a in range(0,max_page):\n",
    "    ###点击详情页下载\n",
    "    all_当前页面的所有文章=driver.find_elements_by_xpath('//td[@class=\"name\"]//a')\n",
    "    sleep(3)\n",
    "    for b in range(len(all_当前页面的所有文章)):\n",
    "        element=driver.find_elements_by_xpath('//td[@class=\"name\"]//a')[b]\n",
    "        print(element.get_attribute('innerHTML'))\n",
    "        element.click()\n",
    "        driver.switch_to_window(driver.window_handles[2])\n",
    "        sleep(2)\n",
    "        path = \"D:\\知网_pdf\"      # 输入文件夹地址（更改为自己浏览器下载文件夹）\n",
    "        files = os.listdir(path)   # 读入文件夹\n",
    "        num = len(files) \n",
    "        element=driver.find_element_by_xpath('//*[@id=\"pdfDown\"]')\n",
    "        driver.execute_script(\"arguments[0].click();\", element)\n",
    "        sleep(8)\n",
    "        nums= len(os.listdir(path))\n",
    "        ### 查看是否有新增下载文件出现    \n",
    "        if nums>num:\n",
    "            driver.close()\n",
    "            sleep(2)\n",
    "            driver.switch_to_window(driver.window_handles[1])\n",
    "        ### 无则进行是否有验证码页面的判定\n",
    "        else:\n",
    "            driver.switch_to_window(driver.window_handles[-1])\n",
    "            sleep(1)\n",
    "            driver.switch_to_window(driver.window_handles[-2])\n",
    "            sleep(1)\n",
    "            driver.switch_to_window(driver.window_handles[-1])\n",
    "            try:\n",
    "                driver.find_element_by_xpath ('//*[@id=\"vImg\"]')\n",
    "                verification_code()\n",
    "            ### 没出现验证码则关闭当前窗口退回\n",
    "            except:\n",
    "                driver.close()\n",
    "                driver.switch_to_window(driver.window_handles[1])###回到链接页面\n",
    "    element=driver.find_element_by_xpath('//*[@id=\"PageNext\"]')\n",
    "    element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "307.193px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
